import socket


'''凯撒密码加密'''
def encrypt(text, shift):
    result = ""
    for char in text:
        if char.isalpha():
            start = ord('A') if char.isupper() else ord('a')
            result += chr((ord(char) - start + shift) % 26 + start)
        else:
            result += char
    return result


'''凯撒密码解密'''
def decrypt(text, shift):
    return encrypt(text, -shift)


client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
'''连接服务器'''
client_socket.connect(("192.168.190.2", 9999))

shift = 3
while True:
    choice = input("输入 1 从文件读取内容发送，输入 2 手动输入内容发送，输入 q 退出：")
    if choice == 'q':
        break
    elif choice == '1':
        try:
            with open('input.txt', 'r') as file:
                content = file.read()
            encrypted_content = encrypt(content, shift)
            client_socket.send(encrypted_content.encode())
        except FileNotFoundError:
            print("文件未找到，请确保 input.txt 文件存在。")
    elif choice == '2':
        message = input("zk 说：")
        encrypted_message = encrypt(message, shift)
        client_socket.send(encrypted_message.encode())

    encrypted_data = client_socket.recv(1024).decode()
    decrypted_data = decrypt(encrypted_data, shift)
    print("wk 回答：", decrypted_data)

client_socket.close()